package cz.muni.fi.pa036.kiss.serviceimpl;

import cz.muni.fi.pa036.kiss.Entity.StudentDTO;
import cz.muni.fi.pa036.kiss.Entity.Zamestnanec;
import cz.muni.fi.pa036.kiss.Entity.ZamestnanecAssembler;
import cz.muni.fi.pa036.kiss.Entity.ZamestnanecDTO;
import cz.muni.fi.pa036.kiss.dao.ZamestnanecDAO;
import cz.muni.fi.pa036.kiss.service.ZamestnanecService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

public class ZamestnanecServiceImpl implements ZamestnanecService {

    @Autowired
    private ZamestnanecDAO zamestnanecDao;
    @Autowired
    private ZamestnanecAssembler assembler;

    @Transactional
    @Override
    public void createZamestnanec(ZamestnanecDTO zamestnanec) {
        if (zamestnanec == null) {
            throw new IllegalArgumentException("zamestnanec is null: ");
        }
        if (zamestnanec.getId() != null) {
            throw new IllegalArgumentException("id is not null");
        }
        Zamestnanec zam = assembler.createEntityfromDTO(zamestnanec);
        zamestnanecDao.createZamestnanec(zam);
        zamestnanec.setId(zam.getId());
    }

    @Transactional(readOnly = true)
    @Override
    public ZamestnanecDTO getZamestnanec(Long Id) {
        if (Id == null) {
            throw new IllegalArgumentException("id is null");
        }
        ZamestnanecDTO zamestnanec = assembler.createDTOfromEntity(zamestnanecDao.getZamestnanec(Id));
        if (zamestnanec == null) {
            return null;
        } else {
            return zamestnanec;
        }
    }

    @Transactional
    @Override
    public void removeZamestnanec(ZamestnanecDTO zamestnanec) {
        if (zamestnanec == null) {
            throw new IllegalArgumentException("zamestnanec is null");
        }
        zamestnanecDao.removeZamestnanec(assembler.createEntityfromDTO(zamestnanec));
    }

    @Transactional
    @Override
    public void updateZamestnanec(ZamestnanecDTO zamestnanec) {
        if (zamestnanec == null) {
            throw new IllegalArgumentException("zamestnanec is null");
        }
        zamestnanecDao.updateZamestnanec(assembler.createEntityfromDTO(zamestnanec));
    }

    @Transactional(readOnly = true)
    @Override
    public List<ZamestnanecDTO> findAll() {
        List<Zamestnanec> all = zamestnanecDao.findAll();
        List<ZamestnanecDTO> allDto = new ArrayList<ZamestnanecDTO>();
        for (Zamestnanec zamestnanec : all) {
            allDto.add(assembler.createDTOfromEntity(zamestnanec));
        }
        return allDto;
    }

    @Transactional(readOnly = true)
    @Override
    public List<ZamestnanecDTO> findByName(String jmeno, String prijmeni) {
        if (jmeno == null && prijmeni == null) {
            throw new IllegalArgumentException("jmeno & prijmeni is null");
        }
        List<Zamestnanec> all = zamestnanecDao.findByName(jmeno, prijmeni);
        List<ZamestnanecDTO> allDto = new ArrayList<ZamestnanecDTO>();
        for (Zamestnanec zamestnanec : all) {
            allDto.add(assembler.createDTOfromEntity(zamestnanec));
        }
        return allDto;
    }
}
